//
//  GRXAllProductCell.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 15/12/25.
//  Copyright © 2015年 grx. All rights reserved.
//

#import "GRXAllProductCell.h"
#import "GRXHeaderCollectionViewCell.h"
#import "GRXLoadDataTool.h"
#import "GRXCategoryItem.h"
#import <MJExtension/MJExtension.h>
@interface GRXAllProductCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/**全部商品模型数组*/
@property (nonatomic,strong)NSArray *allItemArray;

/**<#注释#>*/
@property (nonatomic, weak) UICollectionView *collectionView;


@end

@implementation GRXAllProductCell
static NSString * const productCell = @"product";
- (NSArray *)allItemArray
{
    if (!_allItemArray) {
        _allItemArray = [[NSArray alloc] init];
    }
    return _allItemArray;
}

- (void)awakeFromNib{
    
    [self setCollectionView];
    
    //加载数据
    [self loadData];
}

- (void)loadData{
    [GRXLoadDataTool loadData:^(NSDictionary *htmlData) {
        self.allItemArray = [GRXCategoryItem mj_objectArrayWithKeyValuesArray:htmlData[@"categories"][@"list"]];
        
        self.titleLabel.text = htmlData[@"categories"][@"title"];
        
        //刷新数据
        [self.collectionView reloadData];
        
    }];
}



- (void)setCollectionView{
    //创建流水布局
    CGFloat cellW = kScreenW / 4.0;
    CGFloat cellH = 124;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置流水布局属性
    //2.1 设置cell尺寸
    flowLayout.itemSize = CGSizeMake(cellW, cellH);
    
    //设置cell间距
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    

    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), kScreenW, 124 * 4) collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    
    //设置数据源
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    self.collectionView = collectionView;
    
    // 4.不允许collectionView滚动
    // 取消弹簧效果
    //        collectionView.bounces = NO;
    collectionView.scrollEnabled = NO;
    
    
    //注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"GRXHeaderCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:productCell];
    
    [self addSubview:collectionView];
    
}

#pragma mark ---UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allItemArray.count;
}


//- (UICollectionViewCell)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GRXHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:productCell forIndexPath:indexPath];
    
    GRXCategoryItem *item = self.allItemArray[indexPath.item];
    cell.categoryItem = item;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GRXCategoryItem *item = self.allItemArray[indexPath.item];
    
    NSString *url = item.link;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:GRXJumpDetailControllerNotification object:nil userInfo:@{@"url" : url}];
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
